<?php
include_once "RestRequest.inc.php";
class Am_Apineo{
	protected $api_key;
	protected $api_switch;
	protected $api_type;
	protected $data_send;
	protected $project_id;
	public function __construct()
	{
		$this->api_key = Am_Di::getInstance()->config->getAPIKey();
	}
	
	//get set
	public function setAPIFull($api_type, $api_switch)
	{
	
		$this->api_key = Am_Di::getInstance()->config->getAPIKey();
		$this->api_switch = $api_switch;
		$this->api_type = $api_type;
		$this->data_send = array(
			'key' => $this->api_key,
			'type' => $this->api_type,
			'switch' => $this->api_switch
		);
	} 
	public function setAPIKey($api_key)
	{
		$this->api_key = $api_key;
	}
	public function getAPIKey()
	{
		return $this->api_key;
	}
	public function setAPISwitch($api_switch)
	{
		$this->api_switch = $api_switch;
	}
	public function getAPISwitch()
	{
		return $this->api_switch;
	}
	public function setAPIType($api_type)
	{
		$this->api_type = $api_type;
	}
	public function getAPIType()
	{
		return $this->api_type;
	}
	public function setAPIProjectID($project_id)
	{
		$this->project_id = $project_id;
	}
	public function getAPIProjectID()
	{
		return $this->project_id;
	}
	//End get set	
	
	//action
	public function getAPIData()
	{
		$url = 'http://localhost/ci/api/clients?key=' . $this->api_key . '&sw=' . $this->api_switch . "&type=" . $this->api_type . "&project_id=" . $this->project_id ;
		//print_r($url); die();
		
		
		$request_get = new RestRequest($url);
		$request_get->execute(); 
		$result = $request_get->getResponseBody();
		
		$result = json_decode($result);
		
		return $result;
	
	}
	public function checkAPIKey()
	{
		$this->api_switch = 0;
		$this->api_type = 0;
		$result = $this->getAPIData();
		//print_r($result);die();
		if($result == -100)
			return false;
		return true;
	}
	public function insertAPIData($data_insert)
	{
		$this->data_send['data'] = $data_insert;
		
		
		$request = new RestRequest('http://localhost/ci/api/clients', 'PUT', $this->data_send);
		$request->execute();
		$result = $request->getResponseBody();	
	
		return $result;			
	}
	public function updateAPIData($data_update)
	{
		
		$this->data_send['data'] = $data_update;
		
		
		$request = new RestRequest('http://localhost/ci/api/clients', 'POST', $this->data_send);
		$request->execute();
		$result = $request->getResponseBody();	
		return $result;			
	}
	public function deleteAPIData($id)
	{
		$data_del = array(
					"key" => $this->api_key,
					"type" => $this->api_type,
					"id" => $id
			);

		$request = new RestRequest('http://localhost/ci/api/clients', 'DELETE', $data_del);
		$request->execute();
		$result = $request->getResponseBody();	
		return $result;		
	}
	public function scrapData( $sw, $arr_value = null)
	{
		$url_scrap = "";
		switch($sw)
		{
			case 1;
				$data_scrap = array(
					"url" => $arr_value['url']				
				);
				$url_scrap = "http://neobookmark.net/scrap/meta";
				break;
			case 2:
				$data_scrap = array(
					"url" => $arr_value['url']				
				);
				$url_scrap = "http://neobookmark.net/scrap/keytag";
				break;
			case 3:
				$data_scrap = array(
					"urls" => $arr_value['url']			
				);
				$url_scrap = "http://neobookmark.net/scrap/spinurl";
				break;
			case 4:
				$data_scrap = array(
					"char" => $arr_value['char'],
					"keyword" => $arr_value['keyword']				
				);
				$url_scrap = "http://neobookmark.net/scrap/keyword";
				break;
			case 5:
				$data_scrap = array(
					"keyword" => $arr_value['keyword']				
				);
				$url_scrap = "http://neobookmark.net/scrap/spinkey";
				break;
			case 6:
				$data_scrap = array(
					"num" => $arr_value['num'],
					"keyword" => $arr_value['keyword']				
				);
				$url_scrap = "http://neobookmark.net/scrap/description";
				break;
			case 7:
				$data_scrap = array(
					"text" => $arr_value['text'],
									
				);
				$url_scrap = "http://neobookmark.net/scrap/test_spin";
				break;
			
				
		}
		$request = new RestRequest($url_scrap, 'POST', $data_scrap);
		$request->execute();
		$result = $request->getResponseBody();
		//$result = json_decode($result);	
		return $result;			
	}
	
	
}
?>